package com.apps.Interceptor;

import com.alibaba.fastjson.JSON;
import com.apps.Thirdparty.Jwt.config.JwtConfig;
//import com.apps.Thirdparty.Jwt.service.JwtManageService;
import com.apps.helper.MemberConextHelper;
import com.apps.helper.RequestResponseHelper;
import com.apps.helper.ThreadContextHelper;
import com.apps.smodule.model.po.ApiResult;
import com.apps.sutils.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 所有非静态请求拦截器，主要干两个事
 * 1、将所有请求、响应设置到当前线程中,便于随时获取JavaEE对象
 * 2、拦截会员需要登录的请求
 *
 * @author: YuXD
 * @create: 2019-05-20 11:51
 **/
@Data
@Slf4j
//@Component
public class GlobalInterceptor implements HandlerInterceptor {


//    @Autowired
//    private JwtManageService jwtManageService;

//    @Autowired
//    private IAdminRoleAuthorityService adminRoleAuthorityService;
    //用户登录才能调用的接口
    private String[] checkIsMemberLoginApiPathPatterns = {"/api/member/**"};
    //管理员登录才能调用的接口
    private String[] checkIsAdminLoginApiPathPatterns = {"/api/admin/**"};


    public GlobalInterceptor()
    {

    }

    /**
     * 前置拦截器
     * @param request
     * @param response
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception
    {
        // 将所有请求、响应设置到当前线程中
        ThreadContextHelper.setLocal(request, response);
        // 对处于需要进行登录校验的访问请求进行登录校验
        String url = request.getRequestURI();
        //管理员登录校验
//        Admin admin= MemberConextHelper.getCurrentAdmin();
        //如果是必须用户登录的接口则返回调用失败
        if(StringUtils.isContains(checkIsMemberLoginApiPathPatterns,url))
        {
            Object token=request.getAttribute("Authentication");
            //没有传token
            if(null==token || StringUtils.isEmpty(token.toString())) {
                RequestResponseHelper.responseWrite(JSON.toJSONString(new ApiResult<>().dataError(10000, "请先登录")), response);
                return false;
            }
//            //验证token
//            Member member=jwtManageService.verifyJWT(token.toString());
//            //验证是否被注销
//            if(member.getLive().intValue()==0)
//            {
//                RequestResponseHelper.responseWrite(JSON.toJSONString(new ApiResult<>().dataError(10001, "该账户已被注销")), response);
//                return false;
//            }
//            ThreadContextHelper.MemberLogin(member);
        }
        //如果是必须管理登录的接口则返回登录页面
//        if(StringUtils.isContains(checkIsAdminLoginApiPathPatterns,url))
//        {
//            if(null==admin)
//               RequestResponseHelper.responseWrite(JSON.toJSONString(new ApiResult<>().dataError(10000,"请先登录")), response);
//            //校验请求权限
//            if(!adminRoleAuthorityService.verification(url))
//                RequestResponseHelper.responseWrite(JSON.toJSONString(new ApiResult<>().dataError(10008,"您没有权限")), response);
//
//            return false;
//        }


        return true;
    }


    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) {
        return;
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        /*
        从当前请求线程中移除请求、响应、Session对象
         */
        ThreadContextHelper.remove();
    }


}
